<?php

header ('Content-type: text/html; charset=UTF-8'); 

//header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() - 3600));


?>


<script src="system.js"></script>


<script language="javascript" type="text/javascript">



function returnExecuteAllItens(jsonObj){
	
	
	if( jsonObj.status == 1){
	
		alert( jsonObj.data );
		window.location.href='transformation_list.php?view_id='+getURIParameter('view_id');
		
	}else{
		
		alert( jsonObj.data );
	}
	
	
	//document.getElementById("loading").style="visibility:hidden";
}
		

function executeAllItens(view_id) 
{
	
	var r = confirm("Confirma execução de toda as regras?");
	if (r == true)
	{
		
		var oObject = {} ;
						
		oObject["view_id"] = view_id;
		oObject["component"] = "etl";
		oObject["controller"] = "transformation";
		oObject["task"] = "executeAllItens";
		
		
		var jsonVales	=	convertToJSON(oObject);
		
		parameters	=	'parameters='+encodeURIComponent(
								convertToStringJSON(jsonVales));
		
		var method='PUT';
		
		httpRequest ( method,
						url_API,
						parameters,
						'returnExecuteAllItens');

		document.getElementById("loading").style="visibility:visible";
	}
	
}




function returnExecute(jsonObj){
	
	
	if( jsonObj.status == 1){
	
		window.location.href='transformation_list.php?view_id='+getURIParameter('view_id');
		
	}else{
		
		alert( jsonObj.data );
	}
	
	
	//document.getElementById("loading").style="visibility:hidden";
}
		

function executeItem(id) 
{
	
	var r = confirm("Confirma execução?");
	if (r == true)
	{
		
		var oObject = {} ;
						
		oObject["id"] = id;
		oObject["component"] = "etl";
		oObject["controller"] = "transformation";
		oObject["task"] = "execute";
		
		
		var jsonVales	=	convertToJSON(oObject);
		
		parameters	=	'parameters='+encodeURIComponent(
								convertToStringJSON(jsonVales));
		
		var method='PUT';
		
		httpRequest ( method,
						url_API,
						parameters,
						'returnExecute');

		document.getElementById("loading").style="visibility:visible";
	}
	
}



function returnCloneItem(jsonObj){

	
	
	if( jsonObj.status == 1){
	
		window.location.href='transformation_list.php?view_id='+getURIParameter('view_id');
		
	}else{
		
		alert( jsonObj.data );
	}
	
}


function cloneItem(id){
	
	var r = confirm("Confirma clonar?");
	if (r == true)
	{
		var oObject = {} ;
						
		oObject["id"] = id;
		oObject["component"] = "etl";
		oObject["controller"] = "transformation";
		oObject["task"] = "clone";
		
		
		var jsonVales	=	convertToJSON(oObject);
		
		parameters	=	'parameters='+encodeURIComponent(
								convertToStringJSON(jsonVales));
		
		var method='POST';
			
		httpRequest ( method,
						url_API,
						parameters,
						'returnCloneItem');
						
	}
	
}




function deleteItem(id) 
{
	
	var r = confirm("Confirma exclusão?");
	if (r == true)
	{
	
		var oObject = {} ;
		
		oObject["id"] = id;
		oObject["component"] = "etl";
		oObject["controller"] = "transformation";
		oObject["task"] = "delete";
		
		
		var jsonVales	=	convertToJSON(oObject);
		
		parameters	=	'parameters='+encodeURIComponent(
								convertToStringJSON(jsonVales));
		
		var method='DELETE';
			
			
		httpRequest ( method,
						url_API,
						parameters,
						'retornDelete');
						
	}

}

function retornDelete(jsonObj){
	
	if( jsonObj.status == 1){
		
		window.location.href='transformation_list.php?view_id='+getURIParameter('view_id');
			
	}else{
		
		alert( jsonObj.data );
	}
}



function returnListviewData(jsonObj){
	
	
	
	if( jsonObj.status == 1){
		

		var grid_view_html="";
		

		var obj_json = JSON.parse(jsonObj.data);

		var i;
		//alert(obj_json.length);
		var columns="";
		
		
		for (i=0; i < obj_json.length; i++){
		    //var img = new Image();
		    //img.setAttribute("src",obj[i][2] + obj[i][1]);
		    //document.body.appendChild(img);
		    //alert( obj_json[i].name_view );
		    
		    //alert("name="+obj_json[i].name );
		    
		    if( columns==''){
		    	columns = columns +"<tr>";
		    	columns = columns +"<td>#</td>";
		    	
		    	for (var key in obj_json[i]) {
			    	
					columns = columns +"<td>"+key+"</td>";
			    	
				}
				columns = columns +"</tr>";
		    }
		    
			

		    grid_view_html = grid_view_html +"<tr>";
		    grid_view_html = grid_view_html	+"<td style='width:200px;'>"
		    				+"<a href='transformation_edit.php?id="+obj_json[i].id+"&view_id="+getURIParameter('view_id')+"'>Editar</a>"
		    				+"|| <a href='javascript:cloneItem("+obj_json[i].id+");'>Clonar</a>"
		    				+"|| <a href='javascript:executeItem("+obj_json[i].id+")'>Executar </a>"
		    				+"|| <a href='javascript:deleteItem("+obj_json[i].id+");'>Excluir</a>"
		    				+"|| <a target='_blank' href='view_data.php?id="+getURIParameter('view_id')+"'>Dados</a></td>";
		    		    
			
		    for (var key in obj_json[i]) {
			    	
				grid_view_html = grid_view_html +"<td>"+decodeURIComponent(obj_json[i][key])+"</td>";
			    	
			}

		    grid_view_html = grid_view_html +"</tr>";

		}


		document.getElementById("grid_view").innerHTML = "<table border=1>"+columns+grid_view_html+"</table>";
		//document.getElementById("grid_view").innerHTML = "<table border=1><tr><td>#</td><td>id</td><td>nome</td><td>table</td><td>host</td><td>detalhe</td><td>criado</td></tr>"+grid_view_html+"</table>";

		
		
	}else{
		
		alert(jsonObj.data);
		
		if(jsonObj.notification==25){
			window.location.href='index.php';
		}
	}


	
}


function getListView(view_id, column, order, limit, offset) 
{
	
	var oObject = {} ;
					
	oObject["view_id"] = view_id;
	oObject["limit"] = limit;
	oObject["offset"] = offset;
	oObject["column"] = column;
	oObject["order"] = order;
	oObject["component"] = "etl";
	oObject["controller"] = "transformation";
	oObject["task"] = "getList"; 
	
	
	var jsonVales	=	convertToJSON(oObject);
	
	parameters	=	'parameters='+encodeURIComponent(
							convertToStringJSON(jsonVales));
	
	var method='GET';
		
	httpRequest ( method,
					url_API,
					parameters,
					'returnListviewData');
}


</script>


<h1>Lista de Transformações</h1>

<div id="text-result-view" style="visibility:hidden;width:1px;height:1px;"></div>
<div id="text-result-hidden" style="visibility:hidden;width:1px;height:1px;"></div>
<div id="text-result-hidden-username" style="visibility:hidden;width:1px;height:1px;"></div>

<div id="text-result">Area restria aos usuarios autenticados. voce esta logado como <div id="username-id"></div>.<a href="#" onclick="javascript:logout();">Clique aqui para Logout</a></div>

<br>


<a href="home_authenticated.php">Página inicial</a><br>
<a href="view_list.php">Lista de visões</a><br>
<a href="javascript:executeAllItens(getURIParameter('view_id'));">Executar Todas as Regras de Transformação</a><br>
<a href="javascript:window.location.href='transformation_edit.php?view_id='+getURIParameter('view_id');">Nova Transformação</a><br>


<div id="loading" style="visibility: hidden;"><img src="loading.gif"></div>
<div id="grid_view"></div>




    
<script>
getUsername();
getListView(getURIParameter('view_id'),"id","desc",10,0);

</script>